package com.woniu.controller;

import com.woniu.entity.Result;
import com.woniu.entity.vo.CheckInfoVO;
import com.woniu.pojo.Berth;
import com.woniu.pojo.Road;
import com.woniu.service.BerthService;
import com.woniu.service.PatternService;
import com.woniu.utils.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Ma.Lei
 * @description
 * @date 2022/12/29 14:54
 */
@RestController
@RequestMapping("/userBerth")
@Api(tags = "用户泊位控制类")
public class UserBerthController extends BaseController {
    @Autowired
    private BerthService berthService;

    @Autowired
    private PatternService patternService;

    /**
     * 校验车位是否有效
     * @param berthCode
     * @return
     */
    @ApiOperation("校验泊位是否可用")
    @GetMapping("/checkBerth/{berthCode}")
    public Result checkBerth(@PathVariable String berthCode){
        Berth berth = berthService.selectByBerthCode(berthCode);
        if (berth == null){
            return new Result(500,"车位号输入错误或该车位使用中");
        }
        return new Result(200,"可使用该车位");
    }

    @ApiOperation("核对信息")
    @GetMapping("/checkInfo/{berthCode}/{carNumber}")
    public Result checkInfo(@PathVariable String berthCode,@PathVariable String carNumber){
        CheckInfoVO checkInfoVO = new CheckInfoVO();
        checkInfoVO.setBerthCode(berthCode);
        checkInfoVO.setCarNumber(carNumber);
        Road road = berthService.selectRoadByBerthCode(berthCode);
        checkInfoVO.setRoadName(road.getRoadName());
        checkInfoVO.setCreateTime(new Date());
        checkInfoVO.setPatternList(patternService.listRoadPattern(road.getId()));
        return toDataResult(checkInfoVO);
    }
}
